package github.flandre.examplemod.common.block;

import github.flandre.examplemod.common.tileentity.ExampleGuiTileEntity;
import github.flandre.examplemod.core.init.TileEntityInit;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.DirectionProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.util.Direction;
import net.minecraft.util.math.shapes.IBooleanFunction;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

import java.util.stream.Stream;
//Blockbench生成了三个文件 贴图.png .java 和 .json
//json放到models/block中 png放到textures/block中
//json需要修改一下贴图位置
/*
* "textures": {
		"0": "examplemod:block/custom_block",
		"particle": "examplemod:block/custom_block"
	},
* */

//另外blockstates是复制其他方块的
//最后加上物品的贴图


public class luoliBlock extends Block{
    //模型的导入
    //Stream.of开始都是从Blockbench生成的
    private static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
    //创建一个存放模型的
    protected static final Map<Direction, VoxelShape> SHAPES =new HashMap<Direction, VoxelShape>();
    private static final VoxelShape SHAPESE = Stream.of(
            Block.makeCuboidShape(4, 18, 3, 12, 26, 11),
            Block.makeCuboidShape(12, 14, 7, 14, 16, 14),
            Block.makeCuboidShape(2, 14, 7, 4, 16, 14),
            Block.makeCuboidShape(4, 10, 7, 12, 16, 9),
            Block.makeCuboidShape(6, 16, 7, 10, 18, 9),
            Block.makeCuboidShape(10, 0, 7, 12, 10, 9),
            Block.makeCuboidShape(4, 0, 7, 6, 10, 9),
            Block.makeCuboidShape(10, 16, 7, 12, 18, 9),
            Block.makeCuboidShape(4, 16, 7, 6, 18, 9),
            Block.makeCuboidShape(2, 8, 5, 4, 10, 7),
            Block.makeCuboidShape(2, 8, 9, 4, 10, 11),
            Block.makeCuboidShape(12, 8, 9, 14, 10, 11),
            Block.makeCuboidShape(12, 8, 5, 14, 10, 7),
            Block.makeCuboidShape(0, 8, 7, 2, 10, 9),
            Block.makeCuboidShape(6, 14, 5, 10, 16, 7),
            Block.makeCuboidShape(6, 14, 9, 10, 16, 11),
            Block.makeCuboidShape(14, 8, 7, 16, 10, 9),
            Block.makeCuboidShape(2, 10, 7, 5, 14, 9),
            Block.makeCuboidShape(4, 8, 9, 12, 14, 11),
            Block.makeCuboidShape(6, 10, 11, 10, 12, 13),
            Block.makeCuboidShape(6, 10, 3, 10, 12, 5),
            Block.makeCuboidShape(4, 8, 11, 12, 10, 13),
            Block.makeCuboidShape(4, 8, 3, 12, 10, 5),
            Block.makeCuboidShape(4, 10, 5, 12, 14, 7),
            Block.makeCuboidShape(11, 10, 7, 14, 14, 9),
            Block.makeCuboidShape(4, 15, 1, 12, 26, 3),
            Block.makeCuboidShape(2, 20, 9, 4, 26, 11),
            Block.makeCuboidShape(12, 16, 9, 14, 24, 11),
            Block.makeCuboidShape(12, 18, 3, 14, 24, 5),
            Block.makeCuboidShape(2, 18, 3, 4, 24, 5),
            Block.makeCuboidShape(2, 24, 3, 4, 26, 11),
            Block.makeCuboidShape(4, 22, 11, 6, 24, 13),
            Block.makeCuboidShape(8, 22, 11, 12, 24, 13),
            Block.makeCuboidShape(4, 24, 11, 12, 26, 13),
            Block.makeCuboidShape(12, 24, 3, 14, 26, 11),
            Block.makeCuboidShape(4, 26, 3, 12, 28, 11)
    ).reduce((v1, v2) -> VoxelShapes.combineAndSimplify(v1, v2, IBooleanFunction.OR)).get();

    public luoliBlock() {
        super(Properties.create(Material.ROCK).hardnessAndResistance(1.5F,100.0F));
        this.setDefaultState(this.getStateContainer().getBaseState().with(FACING, Direction.NORTH));
        runCalution(SHAPESE);
    }

    public void runCalution(VoxelShape voxelShape)
    {
        for(Direction direction :Direction.values())
        {
            calulateShape(direction,voxelShape);
        }
    }


    //放置方块的朝向 玩家视角的反方向
    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockItemUseContext context) {
        return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite());
    }

    //翻转和镜像
    @Override
    public BlockState rotate(BlockState state, Rotation rot) {
        return state.with(FACING, rot.rotate(state.get(FACING)));
    }

    @Override
    public BlockState mirror(BlockState state, Mirror mirrorIn) {
        return state.rotate(mirrorIn.toRotation(state.get(FACING)));
    }

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(FACING);
    }

    //我也不知道这是啥，照着写就好了
    @Override
    public VoxelShape getShape(BlockState state, IBlockReader reader, BlockPos pos, ISelectionContext context)
    {
        return SHAPES.get(state.get(FACING));
    }


    public static void calulateShape(Direction to,VoxelShape shape)
    {
        VoxelShape[] buffer = new VoxelShape[]{shape, VoxelShapes.empty()};
        int items = (to.getHorizontalIndex() - Direction.NORTH.getHorizontalIndex() + 4);
        for(int i = 0 ; i < items ; i++)
        {
            buffer[0].forEachBox((minX,minY,minZ,maxX,maxY,maxZ)->buffer[1] =
                    VoxelShapes.or(buffer[1],VoxelShapes.create(1-maxZ,minY,minX,1-minZ,maxY,maxX)));
            buffer[0]= buffer[1];
            buffer[1] = VoxelShapes.empty();
        }
        SHAPES.put(to,buffer[0]);
    }


}
